<?php

namespace app\models;

use yii;
use yii\db\Query;
use app\lib\Redis;
/**
 *快递表
 */
class Express extends BaseModel
{
    use Redis;

    private $redis;


    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'express';
        $this->redis = $this->getInstance();
    }

    public function all($where = [], $offset = false, $limit = false, $order = false)
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['id'])) $db->andWhere(['id' => $where['id']]);
        if (!empty($where['pid'])) $db->andWhere(['pid' => $where['pid']]);
        if (!empty($where['invoice_order'])) $db->andWhere(['invoice_order'=>$where['invoice_order']]);
        return $db->all();
    }

    public function one($where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['id'])) {
            $db->andWhere(['id' => $where['id']]);
        }
        if (!empty($where['pid'])) {
            $db->andWhere(['pid' => $where['pid']]);

        }
        return $db->limit(1)->one();
    }

    /**批量添加
     * @param $data
     * @return int
     * @throws yii\db\Exception
     */
    public function insert_bacth_express_fee($data)
    {
        $key = [
            'date', 'express_id', 'name', 'customer_number', 'customer_name', 'invoice_order',
            'country', 'express_channel', 'tradeno', 'to_tradeno'
            , 'num', 'weight', 'per_kg', 'fee', 'remark'
        ];
        return yii::$app->db->
        createCommand()
            ->batchInsert($this->table, $key, $data)
            ->execute();
    }

    public function get_express_size($where)
    {
        $db = new Query();
        $db->select('count(*) as num')->from($this->table);
        if ($where['begin_time'] ?? 0) {
            $db->andwhere(['>=', 'date', $where['begin_time']]);
        }
        if ($where['end_time'] ?? 0) {
            $db->andwhere(['<=', 'date', $where['end_time']]);
        }

        if ($where['express_id'] ?? 0) {
            $db->andWhere(['express_id' => $where['express_id']]);
        }
        if ($where['name'] ?? 0) {
            $db->andWhere(['name' => $where['name']]);
        }
        if ($where['customer_number']) {
            $db->andwhere(['customer_number' => $where['customer_number']]);
        }
        if ($where['customer_name'] ?? 0) {
            $db->andWhere(['customer_name' => $where['customer_name']]);
        }
        if ($where['invoice'] ?? 0) {
            $db->andWhere(['invoice_order' => $where['invoice']]);
        } else {
            $db->andWhere(['split_status' => 1]);
        }
        if ($where['tradeno'] ?? 0) {
            $db->andWhere(['tradeno' => $where['tradeno']]);
        }
        if ($where['type']) {
            $db->andWhere(['type' => $where['type']]);
        }
        if ($where['fee'] ?? 0) {
            $db->andWHere(['>=', 'fee', $where['begin_fee']]);
            $db->andWhere(['<=', 'fee', $where['end_fee']]);
        }
        $info = $db->one();
        return $info ? $info['num'] : 0;
    }

    public function get_caculate_data($where)
    {
        $db = new Query();
        $db->select('SUM(fee) as pay_all')->from($this->table);
        if ($where['begin_time']) {
            $db->andWhere(['>=', 'date', $where['begin_time']]);
        }
        if ($where['end_time']) {
            $db->andwhere(['<=', 'date', $where['end_time']]);
        }

        if ($where['express_id']) {
            $db->andWhere(['express_id' => $where['express_id']]);
        }
        if ($where['name']) {
            $db->andWhere(['name' => $where['name']]);
        }
        if ($where['customer_number']) {
            $db->andwhere(['customer_number' => $where['customer_number']]);
        }
        if ($where['customer_name']) {
            $db->andWhere(['customer_name' => $where['customer_name']]);
        }
        if ($where['invoice']) {
            $db->andWhere(['invoice_order' => $where['invoice']]);
        } else {
            $db->andWhere(['split_status' => 1]);
        }
        if ($where['tradeno']) {
            $db->andWhere(['tradeno' => $where['tradeno']]);
        }
        if ($where['type']) {
            $db->andWhere(['type' => $where['type']]);
        }
        if ($where['fee']) {
            $db->andWHere(['>=', 'fee', $where['begin_fee']]);
            $db->andWhere(['<=', 'fee', $where['end_fee']]);
        }
        return $db->one();
    }

    public function get_express_list($p, $s, $where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if ($where['begin_time'] ?? 0) {
            $db->andwhere(['>=', 'date', $where['begin_time']]);
        }
        if ($where['end_time'] ?? 0) {
            $db->andwhere(['<=', 'date', $where['end_time']]);
        }

        if ($where['express_id'] ?? 0) {
            $db->andWhere(['express_id' => $where['express_id']]);
        }
        if ($where['name'] ?? 0) {
            $db->andWhere(['name' => $where['name']]);
        }
        if ($where['customer_number']) {
            $db->andwhere(['customer_number' => $where['customer_number']]);
        }
        if ($where['customer_name'] ?? 0) {
            $db->andWhere(['customer_name' => $where['customer_name']]);
        }
        if ($where['invoice'] ?? 0) {
            $db->andWhere(['invoice_order' => $where['invoice']]);
        }

        if ($where['tradeno'] ?? 0) {
            $db->andWhere(['tradeno' => $where['tradeno']]);
        }
        if ($where['type']) {
            $db->andWhere(['type' => $where['type']]);
        }
        if ($where['fee'] ?? 0) {
            $db->andWHere(['>=', 'fee', $where['begin_fee']]);
            $db->andWhere(['<=', 'fee', $where['end_fee']]);
        }
        $list = $db->orderBy(['date' => SORT_DESC,'pid'=>SORT_DESC,'id'=>SORT_ASC])->offset(($p - 1) * $s)->limit($s)->all();
        return $list;
    }

    public function get_express_type()
    {
        $redisKey = 'express:type';
        if ($list = $this->redis->get($redisKey)) {
            return json_decode($list, true);
        } else {
            $db = new Query();
            $list = $db->select('*')
                ->from(yii::$app->db->tablePrefix . "express_type")
                ->all();
            $this->redis->setex($redisKey, 3600, json_encode($list));
            return $list;
        }
    }


    /**统计invoice的快递费
     * @param $invoice_arr
     * @return array
     */
    public function get_all_express_money($invoice_arr)
    {
        $db = new Query();
        $db->select('invoice_order,fee,express_id, customer_number')
            ->from($this->table)
            ->where(['in', 'invoice_order', $invoice_arr]);


        $db2 = new Query();
        $list = $db2->select('express_id,fee,invoice_order,customer_number')
            ->from(['a' => $db])
            ->all();
        return $list;
    }


    /**
     *获取客户样品费
     * @param $customerArr
     * @return array
     */
    public function getSampleFee($customerArr): array
    {
        $db = new Query();
        return $db->select('sum(fee) as fee,customer_number')->from($this->table)
            ->where(['type' => 2])
            ->andWhere(['in', 'customer_number', $customerArr])
            ->groupBy('customer_number')
            ->all();
    }

    public function updatePid()
    {
        $sql = 'UPDATE jingsourcing_express set pid=id WHERE pid=0';
        yii::$app->db->createCommand($sql)->execute();
    }

    public function validateExpressMoney($pid)
    {
        $db = new Query();
        $list = $db->select('*')->from($this->table)->where(['pid'=>$pid])->all();
        $sum1 = $sum2 = 0;
        foreach ($list as $v) {
            if ($v['id'] == $v['pid']) {
                $sum1 = $v['fee'];
            } else {
                $sum2 += $v['fee'];
            }
        }
        if ($sum2!=0 && $sum1!= $sum2)
            $moneySame = false;
        else
            $moneySame = true;
        $this->save(['moneySame'=>$moneySame],['pid'=>$pid]);
    }

}
